
//  Copyright (C) 2009-2011 Luca Piccioni
// 
//  This program is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
// 
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
// 
//  You should have received a copy of the GNU General Public License
//  along with this program.  If not, see <http://www.gnu.org/licenses/>.

using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;

namespace Derm.Raster
{
	/// <summary>
	/// Plugin extending ImageCodec image support.
	/// </summary>
	public abstract class ImageCodecPlugin : IImageCodecPlugin
	{
		#region Plugin Identification

		/// <summary>
		/// Plugin name.
		/// </summary>
		/// <remarks>
		/// This name identity the plugin implementation. It shall be unique in the plugin set available at
		/// runtime.
		/// </remarks>
		public abstract string Name { get; }

		/// <summary>
		/// Determine whether this plugin is available for the current process.
		/// </summary>
		/// <returns>
		/// It returns a boolean value indicating whether the plugin is available for the current
		/// process.
		/// </returns>
		public abstract bool CheckAvailability();

		#endregion

		#region Plugin Support Queries

		/// <summary>
		/// Get a list of image formats supported for reading.
		/// </summary>
		public abstract IEnumerable<ImageFormat> SupportedReadImageFormats { get; }

		/// <summary>
		/// Get a list of image formats supported for writing.
		/// </summary>
		public abstract IEnumerable<ImageFormat> SupportedWriteImageFormats { get; }

		/// <summary>
		/// Check whether a file stream is supported for reading.
		/// </summary>
		/// <param name="format">
		/// An <see cref="ImageFormat"/> that specifies the image format to test for read support.
		/// </param>
		/// <returns>
		/// A <see cref="System.Boolean"/> indicating whether <paramref name="format"/> is supported.
		/// </returns>
		public bool IsReadSupported(ImageFormat format)
		{
			foreach (ImageFormat fmt in SupportedReadImageFormats)
				if (fmt == format)
					return (true);

			return (false);
		}

		/// <summary>
		/// Check whether a file stream is supported for reading.
		/// </summary>
		/// <param name="path">
		/// A <see cref="System.String"/>
		/// </param>
		/// <returns>
		/// A <see cref="System.Boolean"/>
		/// </returns>
		/// <remarks>
		/// This routine determine whether a file is supported by examining the file
		/// name extention.
		/// </remarks>
		public bool IsReadSupported(string path)
		{
			ImageFormat format = ImageCodec.GetImageFormatFromExtension(path);

			return (IsReadSupported(format));
		}

		/// <summary>
		/// Check whether a file stream is supported for reading.
		/// </summary>
		/// <param name="format">
		/// An <see cref="ImageFormat"/> that specifies the image format to test for read support.
		/// </param>
		/// <returns>
		/// A <see cref="System.Boolean"/> indicating whether <paramref name="format"/> is supported.
		/// </returns>
		public bool IsWriteSupported(ImageFormat format)
		{
			foreach (ImageFormat fmt in SupportedWriteImageFormats)
				if (fmt == format)
					return (true);

			return (false);
		}

		/// <summary>
		/// Check whether a file stream is supported for reading.
		/// </summary>
		/// <param name="path">
		/// A <see cref="System.String"/>
		/// </param>
		/// <returns>
		/// A <see cref="System.Boolean"/>
		/// </returns>
		/// <remarks>
		/// This routine determine whether a file is supported by examining the file
		/// name extention.
		/// </remarks>
		public bool IsWriteSupported(string path)
		{
			ImageFormat format = ImageCodec.GetImageFormatFromExtension(path);

			return (IsWriteSupported(format));
		}

		/// <summary>
		/// Determine the plugin priority for a certain image format.
		/// </summary>
		/// <param name="format">
		/// An <see cref="ImageFormat"/> that specifies the image format to test for priority.
		/// </param>
		/// <returns>
		/// It returns an integer value indicating the priority of this implementation respect other ones supporting the same
		/// image format. Conventionally, a value of 0 indicates a totally impartial plugin implementation; a value less than 0 indicates
		/// a more confident implementation respect other plugins; a value greater than 0 indicates a fallback implementation respect other
		/// plugins.
		/// 
		/// The base implementation of this routine returns 0.
		/// </returns>
		public virtual int GetPriority(ImageFormat format) { return (0); }
		
		#endregion

		#region Codec Implementation

		/// <summary>
		/// Query image file informations.
		/// </summary>
		/// <returns>
		/// A <see cref="ImageInfo"/>
		/// </returns>
		public abstract ImageInfo QueryImageInfo(Stream fs, ImageCodec.CodecCriteria criteria);
		
		/// <summary>
		/// Load image from file.
		/// </summary>
		/// <param name="fs">
		/// A <see cref="FileStream"/> where the image data is stored.
		/// </param>
		/// <param name="criteria">
		/// A <see cref="ImageCodec.CodecCriteria"/> that specifies parameters for loading an image stream.
		/// </param>
		/// <returns>
		/// An <see cref="Image"/> holding the image data.
		/// </returns>
		public abstract Image Load(Stream fs, ImageCodec.CodecCriteria criteria);

		/// <summary>
		/// Save image pixels to file.
		/// </summary>
		/// <param name="fs">
		/// A <see cref="System.IO.FileStream"/> which stores the image data.
		/// </param>
		/// <param name="image">
		/// A <see cref="Image"/> holding the data to be stored.
		/// </param>
		/// <param name="format">
		/// A <see cref="ImageFormat"/> that specifies the image format to used for saving <paramref name="image"/>.
		/// </param>
		/// <param name="criteria">
		/// A <see cref="ImageCodec.CodecCriteria"/> that specifies parameters for loading an image stream.
		/// </param>
		public abstract void Save(Stream fs, Image image, ImageFormat format, ImageCodec.CodecCriteria criteria);

		#endregion

		#region EXIF Tags

		/// <summary>
		/// Color space EXIF tag.
		/// </summary>
		/// <remarks>
		/// Type: USHORT (ExifColorSpace)
		/// Count: 1
		/// </remarks>
		public const ushort ExifTagColorSpace = 0xA001;

		/// <summary>
		/// ExifTagColorSpace values.
		/// </summary>
		public enum ExifColorSpace : ushort
		{
			/// <summary>
			/// sRGB color space
			/// </summary>
			sRGB =			0x0001,
			/// <summary>
			/// Uncalibrayted color.
			/// </summary>
			Uncalibrated =	0XFFFF,
		}

		/// <summary>
		/// Gamma EXIF tag.
		/// </summary>
		/// <remarks>
		/// Type: RATIONAL
		/// Count: 1
		/// </remarks>
		public const ushort ExifTagGamma = 0xA500;

		#endregion
	};
}